package com.lanhao.aot.carrierintegration.inpost;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.HttpRequest;

/**
 * InPost直接对接
 * @author kunzhou3
 */
public class DirectInPostUtil {
	private static final Logger LOG = LoggerFactory.getLogger(DirectInPostUtil.class);
	
//	public static final String BASE_URL = "https://sandbox-api.paczkomaty.pl";
//	public static final String EMAIL = "lanhaoooo@163.com";
//	public static final String PASSWORD = "Chinaespace1234";
	
	public static final String BASE_URL = "https://api.paczkomaty.pl";
	public static final String EMAIL = "e_space@aliyun.com";
	public static final String PASSWORD = "Espace-12345";
	
	public static final String GEN_CODE_URL = BASE_URL + "/?do=revloggenerateactivecode";
	public static final String TRACK_REPORT_URL = BASE_URL + "/?do=revloggetreport";
	public static void main(String[] args) {
		String rma = UUID.fastUUID().toString(true);
//		String code = genActiveCode(rma);
//		LOG.info("genActiveCode={}", code);
		String report = report("4921124368", "066fb31d42e842dd9331447c6e74e16b");
		LOG.info("report={}", report);
	}
	
	public static String genActiveCode(String rma) {
		
		StringBuilder sb = new StringBuilder();
		sb.append("<paczkomaty>");
		sb.append("<rma>" + rma + "</rma>"); // 唯一编号
		sb.append("<packType>A</packType>"); // 包裹类型
		
		DateTime offsetDay = DateUtil.offsetDay(new Date(), 30);
		String expirationDate = DateUtil.format(offsetDay, DatePattern.UTC_SIMPLE_FORMAT);
		
		sb.append("<expirationDate>" + expirationDate + "</expirationDate>"); // 寄件码到期时间，当前时间往后推30天
		sb.append("<senderPhone>333222111</senderPhone>"); // 寄件者的电话
		sb.append("<senderEmail>it@anhelogistics.com</senderEmail>"); // 寄件者的邮箱
//		sb.append("<returnDescription1>-</returnDescription1>");
		sb.append("<address>");
		sb.append("<name>PL-RTN-AE</name>"); // First name or Company name
		sb.append("<surName>-</surName>"); // Last name or Company name
//		sb.append("<email>-</email>");
		sb.append("<phoneNum>880443244</phoneNum>");
		sb.append("<street>ul.Gorzowska 35</street>"); // 收件人街道地址
		sb.append("<buldingNo>-</buldingNo>"); // 收件人Building number
		sb.append("<flatNo>-</flatNo>"); // 收件人flatNo
		sb.append("<zipCode>66-460</zipCode>"); // 收件人邮编
		sb.append("<town>Kamień Wielki</town>"); // 收件人城镇
//		sb.append("<province>Lubuskie</province>");
		sb.append("</address>");
		sb.append("</paczkomaty>");
		Map<String, Object> m = new HashMap<>();
		m.put("email", EMAIL);
		
		m.put("password", PASSWORD);
		m.put("content", sb.toString());
		
		LOG.info("请求InPost获取寄件码，请求地址url={}，请求参数request={}", GEN_CODE_URL, JSON.toJSONString(m));
		String respBody = HttpRequest.post(GEN_CODE_URL).form(m).execute().body();
		LOG.info("请求InPost获取寄件码，返回结果respBody={}", respBody);
		Map<String, Object> xmlToMap = XmlUtil.xmlToMap(respBody);
		String respJson = JSON.toJSONString(xmlToMap);
		JSONObject respObject = JSON.parseObject(respJson);
		if(respObject.containsKey("error")) {
			String error = respObject.getString("error");
			LOG.info("请求InPost获取寄件码返回失败，失败原因error={}", error);
			return "";
		}
		JSONObject returnObject = respObject.getJSONObject("return");
		String code = returnObject.getString("code");
//		String expirationDate = returnObject.getString("expirationDate");
		LOG.info("请求InPost获取寄件码成功，获取到的寄件码code={}，失效日期截止到expirationDate={}", code, expirationDate);
		return code;
		
	}
	
	public static String report(String code, String rma) {
		StringBuilder sb = new StringBuilder();
		sb.append("<paczkomaty>");
		sb.append("<dateRange>");
		sb.append("<startDate>2023-07-01</startDate>");
		sb.append("<endDate>2023-09-01</endDate>");
		sb.append("</dateRange>");
		sb.append("<paging><limit>10</limit><offset>0</offset></paging>");
		sb.append("<code>" + code + "</code>");
		sb.append("<rma>" + rma + "</rma>");
		sb.append("</paczkomaty>");
		Map<String, Object> m = new HashMap<>();
		m.put("email", EMAIL);
		m.put("password", PASSWORD);
		m.put("content", sb.toString());
		LOG.info("请求InPost获取跟踪号，请求地址url={}，请求参数request={}", TRACK_REPORT_URL, JSON.toJSONString(m));
		String respBody = HttpRequest.post(TRACK_REPORT_URL).form(m).execute().body();
		LOG.info("请求InPost获取跟踪号，返回结果respBody={}", respBody);
		Map<String, Object> xmlToMap = XmlUtil.xmlToMap(respBody);
		String respJson = JSON.toJSONString(xmlToMap);
		LOG.info("请求InPost获取跟踪号，返回结果respJson={}", respJson);
		JSONObject respObject = JSON.parseObject(respJson);
		JSONObject returnObject = respObject.getJSONObject("return");
		JSONObject resultObject = returnObject.getJSONObject("result");
		if (resultObject != null) {
			String packingCode = resultObject.getString("reverseReturnPackCode");
			return packingCode;
		}
		return "";
	}
	
}
